// 通用流式响应测试函数
async function testStream(endpoint, prefix) {
    const status = document.getElementById(prefix + 'Status');
    const output = document.getElementById(prefix + 'Output');
    const btn = event.target;
    
    btn.disabled = true;
    status.textContent = '正在连接...';
    output.textContent = '';
    
    try {
        const response = await fetch(endpoint);
        const reader = response.body.getReader();
        const decoder = new TextDecoder();
        
        status.textContent = '正在接收数据...';
        
        while (true) {
            const { done, value } = await reader.read();
            if (done) break;
            
            // 直接使用原始数据，无需处理pad
            const chunk = decoder.decode(value, { stream: true });
            output.textContent += chunk;
            status.textContent = `已接收 ${output.textContent.split('\n').filter(l => l).length} 个数字`;
        }
        
        status.textContent = '✅ 流完成';
    } catch (error) {
        status.textContent = `❌ 错误: ${error.message}`;
    } finally {
        btn.disabled = false;
    }
}

// SSE事件流测试
function testEvents() {
    const status = document.getElementById('eventsStatus');
    const output = document.getElementById('eventsOutput');
    const btn = event.target;
    
    btn.disabled = true;
    status.textContent = '正在连接SSE...';
    output.textContent = '';
    
    const eventSource = new EventSource('/events');
    let eventCount = 0;
    
    eventSource.onmessage = function(event) {
        output.textContent += `事件: ${event.data}\n`;
        eventCount++;
        status.textContent = `已接收 ${eventCount} 个事件`;
    };
    
    eventSource.onerror = function() {
        status.textContent = '❌ SSE连接错误';
        btn.disabled = false;
        eventSource.close();
    };
    
    setTimeout(() => {
        eventSource.close();
        status.textContent = '✅ 事件流完成';
        btn.disabled = false;
    }, 8000);
}

// AI对话流测试
function testChat() {
    const status = document.getElementById('chatStatus');
    const output = document.getElementById('chatOutput');
    const btn = event.target;
    
    btn.disabled = true;
    status.textContent = '正在连接AI...';
    output.textContent = '';
    
    const eventSource = new EventSource('/chat-stream');
    
    eventSource.addEventListener('message', function(event) {
        try {
            const data = JSON.parse(event.data);
            if (data.type === 'start') {
                status.textContent = 'AI正在思考...';
            } else if (data.type === 'chunk') {
                output.textContent += data.content;
                status.textContent = `正在输入... (${data.index + 1}/${data.total})`;
            }
        } catch (e) {
            output.textContent += event.data;
        }
    });
    
    eventSource.addEventListener('end', function() {
        status.textContent = '✅ AI对话完成';
        btn.disabled = false;
        eventSource.close();
    });
    
    eventSource.onerror = function() {
        status.textContent = '❌ AI对话连接错误';
        btn.disabled = false;
        eventSource.close();
    };
}